package com.ruoyi.safe.controller.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizEnterCheck;
import com.ruoyi.safe.domain.BizEnterTask;
import com.ruoyi.safe.domain.dto.BizEnterCheckDTO;
import com.ruoyi.safe.domain.dto.BizEnterDTO;
import com.ruoyi.safe.domain.dto.EnterCheckDTO;
import com.ruoyi.safe.domain.dto.EntityIdAppDTO;
import com.ruoyi.safe.domain.dto.EntityIdsAppDTO;
import com.ruoyi.safe.service.IBizEnterCheckProjectService;
import com.ruoyi.safe.service.IBizEnterCheckService;
import com.ruoyi.safe.service.IBizEnterTaskService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公司检查Controller
 * 
 * @author ruoyi
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/api/enter/enter")
public class EnterCheckController extends BaseController
{
    @Autowired
    private IBizEnterCheckService bizEnterCheckService;
    @Autowired
    private IBizEnterTaskService bizEnterTaskService;
    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询公司检查列表
     */
    @PostMapping("/list")
    public R<Object> list(@RequestBody BizEnterCheckDTO bizEnterCheckDTO)
    {
        SysUser sysUser = getSysUser(bizEnterCheckDTO.getUserName());
        List<BizEnterCheck> list = bizEnterCheckService.selectBizEnterCheckList(bizEnterCheckDTO, sysUser);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 根据企业检查id查询任务列表
     * @param entityIdAppDTO
     * @return
     */
    @PostMapping(value = "/getList")
    public R<Object> projectListByCheckId(@RequestBody EntityIdAppDTO entityIdAppDTO){
        getSysUser(entityIdAppDTO.getUserName());
        if (entityIdAppDTO.getPageNum() != null && entityIdAppDTO.getPageSize() != null) {
            PageUtils.startPage(entityIdAppDTO.getPageNum(), entityIdAppDTO.getPageSize());
        }
        //根据公司检查id查询项目列表
        List<BizEnterTask> bizEnterTaskList = bizEnterTaskService.selectTaskListByCheckId(entityIdAppDTO.getId());
        return R.ok(new PageInfo<>(bizEnterTaskList));
    }

    /**
     * 公司检查--我参与的检查
     * @param bizEnterCheckDTO
     * @return
     */
    @PostMapping("/myList")
    public R<Object> myList(@RequestBody BizEnterCheckDTO bizEnterCheckDTO)
    {
        SysUser user = getSysUser(bizEnterCheckDTO.getUserName());
        if (bizEnterCheckDTO.getPageNum() != null && bizEnterCheckDTO.getPageSize() != null) {
            PageUtils.startPage(bizEnterCheckDTO.getPageNum(), bizEnterCheckDTO.getPageSize());
        }
        BizEnterCheck bizEnterCheck = new BizEnterCheck();
        BeanUtils.copyProperties(bizEnterCheckDTO, bizEnterCheck);
        List<BizEnterCheck> list = bizEnterCheckService.selectBizEnterMyList(bizEnterCheck, user);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 公司检查--我的待办
     * @param bizEnterCheckDTO
     * @return
     */
    @PostMapping("/myTodo")
    public R<Object> myTodo(@RequestBody BizEnterCheckDTO bizEnterCheckDTO){
        SysUser user = getSysUser(bizEnterCheckDTO.getUserName());
        List<EnterCheckDTO> list = bizEnterCheckService.selectToDoList(bizEnterCheckDTO, user);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 获取公司检查详细信息
     */
    @PostMapping( "/info")
    public AjaxResult getInfo(@RequestBody EntityIdAppDTO entityIdAppDTO)
    {
        getSysUser(entityIdAppDTO.getUserName());
        return success(bizEnterCheckService.selectBizEnterCheckById(entityIdAppDTO.getId()));
    }

    /**
     * 新增公司检查
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizEnterCheckDTO bizEnterCheckDTO)
    {
        SysUser user = getSysUser(bizEnterCheckDTO.getUserName());
        BizEnterCheck bizEnterCheck = new BizEnterCheck();
        BeanUtils.copyProperties(bizEnterCheckDTO, bizEnterCheck);
        return toAjax(bizEnterCheckService.insertBizEnterCheck(bizEnterCheck, user));
    }

    /**
     * 点击发送任务按钮时的新增
     * @param bizEnterDTO
     * @return
     */
    @PostMapping("/insert")
    public AjaxResult addEnter(@RequestBody BizEnterDTO bizEnterDTO){
        SysUser user = getSysUser(bizEnterDTO.getUserName());
        return toAjax(bizEnterCheckService.insertEnter(bizEnterDTO, user));
    }

    /**
     * 修改公司检查
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BizEnterCheckDTO bizEnterCheckDTO)
    {
        SysUser user = getSysUser(bizEnterCheckDTO.getUserName());
        BizEnterCheck bizEnterCheck = new BizEnterCheck();
        BeanUtils.copyProperties(bizEnterCheckDTO, bizEnterCheck);
        return toAjax(bizEnterCheckService.updateBizEnterCheck(bizEnterCheck, user));
    }

    /**
     * 删除公司检查
     */
    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody EntityIdsAppDTO entityIdsAppDTO)
    {
        getSysUser(entityIdsAppDTO.getUserName());
        return toAjax(bizEnterCheckService.deleteBizEnterCheckByIds(entityIdsAppDTO.getIds()));
    }

    private SysUser getSysUser(String userName) {
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", userName));
        }

        return sysUser;
    }
}
